﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AmiEmuLib.Configuration.EventBindingConfig;
using AmiEmuLib.Host.HostInput;
using AmiEmu;

namespace AmiEmu
{
    public partial class EventsMappingForm : Form
    {
        private EventBinding m_event_binding;
        private int m_combination_index;
        private List<HostEvent> m_combination = new List<HostEvent>();
        private bool m_ignore_first_release = true;

        public EventsMappingForm(EventBinding a_event_binding, int a_combination_index)
        {
            InitializeComponent();

            m_event_binding = a_event_binding;
            m_combination_index = a_combination_index;

            if (m_combination_index < m_event_binding.Combinations.Count())
            {
                m_combination.AddRange(
                    m_event_binding.Combinations.ElementAt(m_combination_index).Combination);
            }

            combinationLabel.Text = HostEventsCombination.GetShortcut(m_combination);

            HostInputDevicesList.OnEvent += ProcessEvent;
        }

        private void EventsMappingForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12)
                Ok();
        }

        private void EventsMappingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            HostInputDevicesList.OnEvent -= ProcessEvent;
        }

        private void ProcessEvent(HostEvent a_event, bool a_pushed, int a_value)
        {
            if (Focused)
            {
                if (a_event == HostEventsList.MouseEvents.GetEvent(
                    HostMiceDevices.Event_ID_Left_Button))
                {
                    var pt = PointToClient(Control.MousePosition);
                    if (okPanel.Bounds.Contains(pt) || cancelPanel.Bounds.Contains(pt) ||
                        resetPanel.Bounds.Contains(pt))
                    {
                        return;
                    }
                }
            }

            if (m_combination.Contains(a_event))
                return;
            
            m_combination.Add(a_event);

            if (m_combination.Count > HostEventsCombination.MAX_EVENTS_COUNT)
                m_combination.RemoveAt(0);

            if ((!a_pushed) && (m_ignore_first_release))
            {
                m_ignore_first_release = false;
                return;
            }

            combinationLabel.Text = HostEventsCombination.GetShortcut(m_combination);
        }

        private void Reset()
        {
            m_combination.Clear();
            combinationLabel.Text = HostEventsCombination.GetShortcut(m_combination);
        }

        private void Ok()
        {
            m_event_binding.UpdateCombination(m_combination_index, m_combination);
            Close();
        }

        private void resetLabel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void cancelLabel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void okLabel_MouseEnter(object sender, EventArgs e)
        {
            ((sender as Label).Parent as Panel).BackColor = 
                System.Drawing.SystemColors.ControlLightLight;
        }

        private void okLabel_MouseLeave(object sender, EventArgs e)
        {
            ((sender as Label).Parent as Panel).BackColor = 
                System.Drawing.SystemColors.Control;
        }

        private void okLabel_Click(object sender, EventArgs e)
        {
            Ok();
        }
    }
}
